package cn.hk.query.service;

import cn.HK.po.User;
import cn.HK.utils.MapperUtil;
import cn.hk.query.mapper.QueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


import java.util.concurrent.TimeUnit;
@Service
public class QueryService {
    @Autowired(required = false)
    private QueryMapper queryMapper;
    /*
     * 查询 个人本人全部信息
     * 1.去数据库通过id获取全部user
     *   1.1没有对应id返回null
     *   1.2有对应id、返回相应个人信息
     * */
    public User queryOneUser(Integer u_id){
        User exist = queryMapper.selectUserById(u_id);
        if (exist==null){
            //没有查到 返回null
            return null;
        }else {
            //查到对应id
            System.out.println(queryMapper.selectUserById(u_id));
            return queryMapper.selectUserById(u_id);
        }

    }




    /*//mapper 注入
    @Autowired(required = false)
    private QueryMapper queryMapper;
    @Autowired
    private  StringRedisTemplate redisTemplate;
    // private static final ObjectMapper mapper=new ObjectMapper();
    public User queryOneUser(Integer u_id) {


    }

        ValueOperations<String, String> opsForString= redisTemplate.opsForValue();
        String productKey = "user_" + u_id;
        try {
            //判断命中
            if (redisTemplate.hasKey(productKey)) {
                String pJson=opsForString.get(productKey);
                User user= MapperUtil.MP.readValue(pJson,User.class);
                return user;
            } else {
                //缓存没数据
                User u = queryMapper.selectUserById(u_id);
                //
                String pJson= MapperUtil.MP.writeValueAsString(u);
                opsForString.set(productKey,pJson,2, TimeUnit.DAYS);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return queryMapper.selectUserById(u_id);
    }*/


}
